package com.netintech.common.utils.hk;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * 海康api工具类
 *
 * @author zhushanshuo
 */
@Component
public class ApiUtils {
	
	private static final Logger logger = LoggerFactory.getLogger(ApiUtils.class);

	//服务器IP
	private String hostStr = "";

	//应用key
	private String appkey = "";

	//应用密钥
	private String appsecrect = "";

	//apiIP
	private String apiIp = "";

	//api端口
	private String apiPort = "4433";

	private String httpMethod = "https";

	public String getJSONDataByApi(JSONObject jsonBody, String url, String requestPath) {
		logger.info("请求地址："+url);
		logger.info("请求参数："+jsonBody.toJSONString());
		/**
		 * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
		 */
		ArtemisConfig.host = hostStr; // artemis网关服务器ip端口
		ArtemisConfig.appKey = appkey; // 秘钥appkey
		ArtemisConfig.appSecret = appsecrect;// 秘钥appSecret

		/**
		 * STEP2：设置OpenAPI接口的上下文
		 */
		final String ARTEMIS_PATH = "/artemis";
		/**
		 * STEP3：设置接口的URI地址
		 */
		final String previewURLsApi = ARTEMIS_PATH + url;
		@SuppressWarnings("serial")
		Map<String, String> path = new HashMap<String, String>(2) {
			{
				put(httpMethod + "://", previewURLsApi);// 根据现场环境部署确认是http还是https
			}
		};
		/**
		 * STEP4：设置参数提交方式
		 */
		String contentType = "application/json";
		String body = jsonBody.toJSONString();
		/**
		 * STEP6：调用接口
		 */
		String result = "";
		if (StringUtils.isNotEmpty(requestPath) && requestPath.equals("GET")) {
			//第二种方式  
	        Map<String, String> querys = new HashMap<String, String>();
			try {
				Map<String, Object> querys_temp = JSON.parseObject(body);  
				querys = new HashMap<String, String>();
				for (String key:querys_temp.keySet()) {
					String object = (String) querys_temp.get(key);
					querys.put(key, object);
				}
				result = ArtemisHttpUtil.doGetArtemis(path, querys, null, contentType, null);
			} catch (Exception e) {
				logger.error("参数转换异常！",e);
				result = "参数转换异常！";
			}
		}else {
			result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType, null);// post请求application/json类型参数
		}
		logger.info("返回参数："+result);
		return result;
	}

	/**
	 * 获取端口号
	 *
	 * @param href 网址, ftp, http, nntp, ... 等等
	 * @return
	 * @throws IOException
	 */
	public int parsePort(String href) throws IOException {
		// java.net中存在的类
		URL url = new URL(href);
		// 端口号; 如果 href 中没有明确指定则为 -1
		int port = url.getPort();
		if (port < 0) {
			// 获取对应协议的默认端口号
			port = url.getDefaultPort();
		}
		return port;
	}

	/**
	 * 获取Host部分
	 *
	 * @param href 网址, ftp, http, nntp, ... 等等
	 * @return
	 * @throws IOException
	 */
	public String parseHost(String href) throws IOException {
		//
		URL url = new URL(href);
		// 获取 host 部分
		String host = url.getProtocol();
		return host;
	}

	/**
	 * 根据域名(host)解析IP地址
	 *
	 * @param host 域名
	 * @return
	 * @throws IOException
	 */
	public String parseIp(String host) throws IOException {
		URL url = new URL(host);
		String address = url.getHost();
		return address;
	}

	public String getHostStr() {
		return hostStr;
	}

	public void setHostStr(String hostStr) {
		this.hostStr = hostStr;
	}

	public String getAppkey() {
		return appkey;
	}

	public void setAppkey(String appkey) {
		this.appkey = appkey;
	}

	public String getAppsecrect() {
		return appsecrect;
	}

	public void setAppsecrect(String appsecrect) {
		this.appsecrect = appsecrect;
	}

	public String getHttpMethod() {
		return httpMethod;
	}

	public void setHttpMethod(String httpMethod) {
		this.httpMethod = httpMethod;
	}

	public String getApiIp() {
		return apiIp;
	}

	public void setApiIp(String apiIp) {
		this.apiIp = apiIp;
	}

	public String getApiPort() {
		return apiPort;
	}

	public void setApiPort(String apiPort) {
		this.apiPort = apiPort;
	}
	
	
	

}
